package com.tbruyelle.rxpermissions2;

import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.Location;
import android.location.LocationManager;
import android.media.MediaRecorder;
import android.os.Build;
import android.os.Environment;
import android.provider.ContactsContract;
import android.telephony.TelephonyManager;

import java.io.File;
import java.util.List;

/**
 * Created by ytf on 2018/9/14.
 * Description:
 */
public class PermissionChecker
{
    public static boolean hasCameraPermission(Context context)
    {
        if (!checkCameraHardware(context))
        {
            return false;
        }
        try
        {
            Camera camera = Camera.open();
            camera.release();
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    private static boolean checkCameraHardware(Context context)
    {
        return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA);
    }

    public static boolean hasExternalSDCardPermission(Context context)
    {
        try
        {
            //没有权限时通过这种方式触发权限申请
            File f = Environment.getExternalStorageDirectory();
            f = new File(f, "/d07.p");

            if (!f.exists())
            {
                f.createNewFile();
            }else
            {
                f.delete();
            }

            return f.canWrite();

        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean hasMicPermission(Context ctx)
    {
        try
        {
            MediaRecorder recorder = new MediaRecorder();
            recorder.setAudioChannels(1);
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            recorder.setOutputFile(new File(ctx.getCacheDir(), "/touch.3gp").getAbsolutePath());
            recorder.prepare();
            recorder.release();
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean hasReadPhonePermission(Context context)
    {
        try
        {
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            String imei = telephonyManager.getDeviceId();
            return true;
        } catch (Exception e)
        {
            return false;
        }
    }

    public static boolean hasLocationPermission(Context context)
    {
        try
        {
            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            String provider = judgeProvider(lm);
            if (provider != null)
            {
                Location loca = lm.getLastKnownLocation(provider);
                return true;
            } else
            {
                return false;
            }
        } catch (Exception e)
        {
            return false;
        }
    }

    private static String judgeProvider(LocationManager locationManager)
    {
        List<String> prodiverlist = locationManager.getProviders(true);
        if (prodiverlist.contains(LocationManager.NETWORK_PROVIDER))
        {
            return LocationManager.NETWORK_PROVIDER;//网络定位
        } else if (prodiverlist.contains(LocationManager.GPS_PROVIDER))
        {
            return LocationManager.GPS_PROVIDER;//GPS定位
        } else
        {
            return null;
        }
    }

    public static boolean hasReadContactsPermission(Context context)
    {
        Cursor cursor = null;
        try
        {
            cursor = context.getContentResolver()
                    .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, null, null, null);
            if (cursor != null)
            {
                if (cursor.moveToNext())
                {
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                }
                cursor.close();
            }

            return true;
        } catch (Exception e)
        {
            if (cursor != null)
            {
                cursor.close();
            }
            return false;
        }
    }

    public static boolean hasSensorPermission(Context context)
    {
        try
        {
            SensorManager sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
            Sensor sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
            sensor.getResolution();
            return true;
        }catch (Exception e)
        {
            return false;
        }
    }

    public static boolean hasInstallPermission(Context context)
    {
        return Build.VERSION.SDK_INT < 26 || context.getPackageManager().canRequestPackageInstalls();
    }
}
